package com.hmsproject.livedata;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hmsproject.datastructures.Hotel;
import com.hmsproject.datastructures.deal.Deal;
import com.hmsproject.datastructures.transactions.Transaction;
import com.hmsproject.datastructures.transactions.UserPreferences;
import com.hmsproject.db.inmemorydb.HMSInMemoryDBConnection;
import com.hmsproject.db.inmemorydb.TransactionMap;

public class LiveDealData {
	
	public Hotel getHotel() {
		return hotel;
	}
	public int getAlreadyBooked() {
		return alreadyBooked;
	}
	public int getNoOfDaysLeft() {
		return noOfDaysLeft;
	}
	public int getCurrentPrice() {
		return currentPrice;
	}
	public List<LiveDealCriterion> getLiveDealCriterions() {
		return liveDealCriterions;
	}
	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}
	public void setAlreadyBooked(int alreadyBooked) {
		this.alreadyBooked = alreadyBooked;
	}
	public void setNoOfDaysLeft(int noOfDaysLeft) {
		this.noOfDaysLeft = noOfDaysLeft;
	}
	public void setCurrentPrice(int currentPrice) {
		this.currentPrice = currentPrice;
	}
	public void setLiveDealCriterions(List<LiveDealCriterion> liveDealCriterions) {
		this.liveDealCriterions = liveDealCriterions;
	}
	private Hotel hotel;
	private int alreadyBooked;
	private int noOfDaysLeft;
	private int currentPrice;
	private int originalPrice;
	private String userId = "1";
	private String dealUrl;
	private int totalDays;	
	public int getTotalDays() {
		return totalDays;
	}
	public void setTotalDays(int totalDays) {
		this.totalDays = totalDays;
	}
	public String getDealUrl() {
		return dealUrl;
	}
	public void setDealUrl(String dealUrl) {
		this.dealUrl = dealUrl;
	}
	
	public int getTotalCost(){
		return this.noOfRooms * this.currentPrice*totalDays;
	}
	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public int getOriginalPrice() {
		return originalPrice;
	}
	public void setOriginalPrice(int originalPrice) {
		this.originalPrice = originalPrice;
	}
	private List<LiveDealCriterion> liveDealCriterions;
	private Deal deal;
	public Deal getDeal() {
		return deal;
	}
	public void setDeal(Deal deal) {
		this.deal = deal;
	}
	
	private int noOfRooms;
	
	public int getNoOfRooms() {
		return noOfRooms;
	}
	public void setNoOfRooms(int noOfRooms) {
		this.noOfRooms = noOfRooms;
	}
	
	public LiveDealData fireNoOfRoomChanged(){
		
		LiveDealData freshLiveDealData = HMSInMemoryDBConnection.getInstance().getLiveDealData(this.getDeal().getDealId());
		List<LiveDealCriterion> liveDealCriterions = freshLiveDealData.getLiveDealCriterions();
		List<LiveDealCriterion> tobeRemovedLiveDealCriterions = new ArrayList<LiveDealCriterion>();
		//System.out.println("GET NO OF ROOMS " + freshLiveDealData.getNoOfRooms());
		int noOfRoomsBooked = this.getNoOfRooms();
		int additionalpercent = 0;
		for(int i = 0; i < liveDealCriterions.size(); i++){
			LiveDealCriterion currentLiveDealCriterion = liveDealCriterions.get(i);
			
			if(currentLiveDealCriterion.getNoOfRoomsMore() > noOfRoomsBooked){
				currentLiveDealCriterion.setNoOfRoomsMore(currentLiveDealCriterion.getNoOfRoomsMore() - noOfRoomsBooked);
				freshLiveDealData.setAfterDiscount(additionalpercent);
				break;
			}
			additionalpercent += currentLiveDealCriterion.getAdditionalPercentOff();
			tobeRemovedLiveDealCriterions.add(currentLiveDealCriterion);
			noOfRoomsBooked = noOfRoomsBooked - currentLiveDealCriterion.getNoOfRoomsMore();
		}
		
		liveDealCriterions.removeAll(tobeRemovedLiveDealCriterions);
		
		freshLiveDealData.setAfterDiscount(additionalpercent);
		
		int originalPrice = freshLiveDealData.getOriginalPrice();
		freshLiveDealData.setCurrentPrice(freshLiveDealData.getCurrentPrice() - originalPrice*additionalpercent/100);
		freshLiveDealData.setNoOfRooms(this.getNoOfRooms());
		
		
		return freshLiveDealData;
	}
	
	private int availableForBooking = 40;
	public int getAvailableForBooking() {
		return availableForBooking;
	}
	public void setAvailableForBooking(int availableForBooking) {
		this.availableForBooking = availableForBooking;
	}
	
	private int afterDiscount;
	public int getAfterDiscount() {
		return afterDiscount;
	}
	public void setAfterDiscount(int afterDiscount) {
		this.afterDiscount = afterDiscount;
	}

	public Date getFromDate() {
		return fromDate;
	}
	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}
	public Date getToDate() {
		return toDate;
	}
	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}
	private Date fromDate;
	private Date toDate;	
	
}
